
#include "guirenderer.h"

#include <iostream>

#include "gui/gui.h"

#include "renderer/shader.h"

#include "renderer/renderer.h"

#include "gamestate.h"
#include "model.h"
#include "resourcemanager.h"


GuiRenderer::GuiRenderer(int width, int height, float frustumScale, float zNear, float zFar):SubRenderer(width, height, frustumScale, zNear, zFar){
    std::vector<std::string> drawUniformList{"orthoMatrix", "offset", "obscuror"};
    std::vector<std::string> pickUniformList{"orthoMatrix", "offset", "color"};
    InitialiseShaderProgram("gui", drawUniformList);
    InitialiseShaderProgram("gui_picking", pickUniformList);

    ///TODO fix it so that ortho matrix does the tranlsation too
    float orthoMatrix[] = {2.0f/width, 0, 0, 0,
                           0, 2.0f/height, 0, 0,
                           0, 0, -2.0f/100, 0,
                           0, 0, 0, 1};

    std::shared_ptr<Shader> shader = GetShader("gui");
    shader->SetUniform("orthoMatrix", 16, orthoMatrix);
    shader = GetShader("gui_picking");
    shader->SetUniform("orthoMatrix", 16, orthoMatrix);
}

void GuiRenderer::Draw(){
    std::shared_ptr<Shader> shader = GetShader("gui");
    if(shader==nullptr){
        std::cout<<"Shader name invalid\n";
        return;
    }

    glUseProgram(shader->GetProgram());

    gui->Draw();

    glUseProgram(0);
}

void GuiRenderer::Pick(){
    std::shared_ptr<Shader> shader = GetShader("gui_picking");
    if(shader==nullptr){
        std::cout<<"Shader name invalid\n";
        return;
    }

    glUseProgram(shader->GetProgram());

    gui->Pick();

    glUseProgram(0);
}
